<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品编号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入产品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="种植地块" prop="productLand">
        <el-select v-model="queryParams.productLand" placeholder="请选择种植地块" @keyup.enter.native="handleQuery">
          <el-option :value="item.landId" :label="item.landName" :key="item.landId" v-for="item in landList"/>
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择产品类型" clearable size="small">
          <el-option :label="item.dictLabel" :value="item.dictValue" :key="item.dictValue" v-for="item in productTypeList" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="预计产量" prop="productEstimateCount">
        <el-input
          v-model="queryParams.productEstimateCount"
          placeholder="请输入预计产量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="productSize">
        <el-input
          v-model="queryParams.productSize"
          placeholder="请输入产品规格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品重量" prop="productWeight">
        <el-input
          v-model="queryParams.productWeight"
          placeholder="请输入产品重量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保质期" prop="productGuaranteePeriod">
        <el-input
          v-model="queryParams.productGuaranteePeriod"
          placeholder="请输入保质期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存储方式" prop="productStorage">
        <el-input
          v-model="queryParams.productStorage"
          placeholder="请输入存储方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产许可证" prop="productPermit">
        <el-input
          v-model="queryParams.productPermit"
          placeholder="请输入生产许可证"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片地址" prop="productPic">
        <el-input
          v-model="queryParams.productPic"
          placeholder="请输入图片地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="负责人" prop="productPerson">
        <el-input
          v-model="queryParams.productPerson"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="负责人联系方式" prop="productPersonPhone">
        <el-input
          v-model="queryParams.productPersonPhone"
          placeholder="请输入负责人联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['beyond:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['beyond:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['beyond:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['beyond:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="productId" /> -->
      <el-table-column label="产品编号" align="center" prop="productNo" >
        <template slot-scope="scope">
          <div style="color:blue;text-decoration:underline;cursor:pointer;" @click="handleUpdate(scope.row)">
          {{scope.row.productNo }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="种植地块" align="center" prop="productLand" :formatter="filterLand"/>
      <el-table-column label="产品类型" align="center" prop="productType" :formatter="filterProductType"/>
      <el-table-column label="预计产量" align="center" prop="productEstimateCount" />
      <el-table-column label="产品规格" align="center" prop="productSize" />
      <el-table-column label="产品重量" align="center" prop="productWeight" />
      <el-table-column label="保质期" align="center" prop="productGuaranteePeriod" />
      <el-table-column label="存储方式" align="center" prop="productStorage" :formatter="filterStoreWay"/>
      <el-table-column label="生产许可证" align="center" prop="productPermit" />
      <!-- <el-table-column label="图片地址" align="center" prop="productPic" /> -->
      <el-table-column label="负责人" align="center" prop="productPerson" />
      <el-table-column label="负责人联系方式" align="center" prop="productPersonPhone" />
      <el-table-column label="产品介绍" align="center" prop="productExplain" />
      <el-table-column label="生产企业" align="center" prop="productCompany" />
      <el-table-column label="生产企业地址" align="center" prop="productCompanyAddress" />
      <el-table-column label="原材料地址" align="center" prop="productMaterialAddress" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['beyond:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['beyond:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="种植地块" prop="productLand">
          <el-select v-model="form.productLand" placeholder="请选择种植地块">
            <el-option :value="item.landId" :label="item.landName" :key="item.landId" v-for="item in landList"/>
          </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-select v-model="form.productType" placeholder="请选择产品类型">
            <el-option :label="item.dictLabel" :value="item.dictValue" :key="item.dictValue" v-for="item in productTypeList" />
          </el-select>
        </el-form-item>
        <el-form-item label="预计产量" prop="productEstimateCount">
          <el-input v-model="form.productEstimateCount" placeholder="请输入预计产量" />
        </el-form-item>
        <el-form-item label="产品规格" prop="productSize">
          <el-input v-model="form.productSize" placeholder="请输入产品规格" />
        </el-form-item>
        <el-form-item label="产品重量" prop="productWeight">
          <el-input v-model="form.productWeight" placeholder="请输入产品重量" />
        </el-form-item>
        <el-form-item label="保质期" prop="productGuaranteePeriod">
          <el-input v-model="form.productGuaranteePeriod" placeholder="请输入保质期" />
        </el-form-item>
        <el-form-item label="存储方式" prop="productStorage">
          <el-select v-model="form.productStorage" placeholder="请选择存储方式">
            <el-option :label="item.dictLabel" :value="item.dictValue" :key="item.dictValue" v-for="item in storeWayList" />
          </el-select>
        </el-form-item>
        <el-form-item label="生产许可证" prop="productPermit">
          <el-input v-model="form.productPermit" placeholder="请输入生产许可证" />
        </el-form-item>
        <el-form-item label="负责人" prop="productPerson">
          <el-input v-model="form.productPerson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人联系方式" prop="productPersonPhone">
          <el-input v-model="form.productPersonPhone" placeholder="请输入负责人联系方式" />
        </el-form-item>
        <el-form-item label="产品介绍" prop="productExplain">
          <el-input v-model="form.productExplain" type="textarea" placeholder="请输入产品介绍" />
        </el-form-item>
        <el-form-item label="生产企业" prop="productCompany">
          <el-input v-model="form.productCompany" placeholder="请输入生产企业" />
        </el-form-item>
        <el-form-item label="生产企业地址" prop="productCompanyAddress">
          <el-input v-model="form.productCompanyAddress" placeholder="请输入生产企业地址" />
        </el-form-item>
        <el-form-item label="原材料地址" prop="productMaterialAddress">
          <el-input v-model="form.productMaterialAddress" placeholder="请输入原材料地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form ref="form2" :inline="true">
          <el-form-item label="产品图片">
            <ImageUpload2 @input="imgInput" :limit="1" :uploadUrl="url" :value="form.productPic"></ImageUpload2>
          </el-form-item>
        </el-form>
      </el-form>
                
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct } from "@/api/trace/product";
import { queryLand } from "@/api/trace/land";
import ImageUpload2 from "@/components/ImageUpload2"

export default {
  components:{
      ImageUpload2
  },
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品信息表格数据
      productList: [],
      // 地块列表
      landList: [],
      //产品类型列表
      productTypeList: [],
      //存储方式列表
      storeWayList: [],
      // 弹出层标题
      title: "",
      // 上传url
      url: "trace/product/picture/upload",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productNo: null,
        productName: null,
        productLand: null,
        productType: null,
        productEstimateCount: null,
        productSize: null,
        productWeight: null,
        productGuaranteePeriod: null,
        productStorage: null,
        productPermit: null,
        productPic: null,
        productPerson: null,
        productPersonPhone: null,
        productExplain: null,
        productCompany: null,
        productCompanyAddress: null,
        productMaterialAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
    this.getLandList();
    this.getProductTypeList();
    this.getStoreWayList();
  },
  methods: {
    /** 查询产品信息列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**查询产品类型列表 */
    getProductTypeList(){
      this.getDicts('sys_product_type').then(response => {
        this.productTypeList = response.data
      })
    },
    /**查询存储方式列表 */
    getStoreWayList(){
      this.getDicts('sys_store_way').then(response => {
        this.storeWayList = response.data
      })
    },
    /**查询地块列表 */
    getLandList(){
      queryLand().then(response => {
        this.landList = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productNo: null,
        productName: null,
        productLand: null,
        productType: null,
        productEstimateCount: null,
        productSize: null,
        productWeight: null,
        productGuaranteePeriod: null,
        productStorage: null,
        productPermit: null,
        productPic: null,
        productPerson: null,
        productPersonPhone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        productCompany: null,
        productCompanyAddress: null,
        productMaterialAddress: null,
        productExplain: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProduct(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$confirm('是否确认删除产品信息编号为"' + productIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProduct(productIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportProduct(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    filterLand(row){
      let arr = this.landList.filter(val=> val.landId == row.productLand)
      if(arr.length>0) return arr[0].landName
    },
    filterProductType(row){
      let arr = this.productTypeList.filter(val=> val.dictValue == row.productType)
      if(arr.length>0) return arr[0].dictLabel
    },
    filterStoreWay(row){
      let arr = this.storeWayList.filter(val=> val.dictValue == row.productStorage)
      if(arr.length>0) return arr[0].dictLabel
    },
    imgInput(value){
      this.form.productPic = value
    },
  }
};
</script>
